<template>
	<view>
		<!-- 收件人手机号 -->
		<view class="tixian">
			<view class="title"><text class="shi"></text>提现金额:</view>
			<input placeholder="提现金额需>=0.3元" type="digit" placeholder-class="litext" v-model="tixianshuru"></input>
		</view>

		<view class="text">
			<view @tap="withDraw()" class="chongtext" hover-class="chongtext2">立即提现</view>
		</view>
		<view class="tips">
			——提现前需要观看一段广告——
		</view>
	</view>
</template>

<script>
	import {
		http
	} from '../../utils/http.js'
	import {
		mapState,
		mapMutations
	} from 'vuex'
	let rewardedVideoAd2=null
	export default {
		data() {
			return {
				tixianshuru: 0,
				rewardedVideoAd2: null
			};
		},
		created() {
			this.tixianshuru = Number(this.userInfo.balance / 100)
		},
		computed: {
			...mapState('m_user', ['userInfo', 'token'])
		},
		onLoad() {
			// console.log(12)
			let that = this
			// console.log(wx.createRewardedVideoAd)
			if (wx.createRewardedVideoAd) {
		
				rewardedVideoAd2 = wx.createRewardedVideoAd({
					adUnitId: 'adunit-b29b3bf23a08a681'
				})
				rewardedVideoAd2.offError();
				rewardedVideoAd2.offClose();
				rewardedVideoAd2.onLoad(() => {
					// console.log('onLoad event emit')

				})
			 rewardedVideoAd2.onError((err) => {
					// console.log('onError event emit', err)
					uni.showToast({
						title: "获取广告失败",
						duration: 2000,
						icon: "error"
					})
					that.withDraw()
				})
				rewardedVideoAd2.onClose((res) => {
					// console.log('onClose event emit', res)

					if (res && res.isEnded) {
						that.withDraw()
					} else {
						uni.showToast({
							title: "未观看完整广告",
							duration: 2000,
							icon: "none"
						})
					}
				})
			}
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo', 'updateToken']),
			createAD(){
				let that=this
				console.log(123456)
				if(rewardedVideoAd2){
					console.log(123)
					rewardedVideoAd2.show().catch(() => {
					rewardedVideoAd2.load()
						.then(() => rewardedVideoAd2.show())
						.catch(err => {
							console.log(err)
							uni.showToast({
								title: "获取广告失败",
								duration: 2000,
								icon: "none"
							})
							that.withDraw()
						})
				})
				}
			},
			toWithDraw() {
				this.createAD()
			},
			async withDraw() {
				let withDrawMoney = Number(this.tixianshuru)
				let that = this
				if (withDrawMoney <= 0) {
					uni.showToast({
						icon: "none",
						title: "提现金额不能为零",
						duration: 2000,
						position: 'bottom'
					})
					return
				}
				if (withDrawMoney * 100 > this.userInfo.balance) {
					uni.showToast({
						icon: "none",
						title: "提现金额大于余额",
						duration: 2000,
						position: 'bottom'
					})
					return
				}
				// 开始提现
				const [err, succ] = await uni.showModal({
					title: '提示',
					content: '确认提现' + withDrawMoney + "元吗？"
				}).catch(err => err)

				if (succ && succ.confirm) {
					// 去提现
					http({
						url: '/withdraw/order/transfer?money=' + withDrawMoney * 100,
						method: "POST",
						header: {
							Authorization: that.token.data,
							'Content-Type': 'x-www-form-urlencoded'
						}
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								icon: "none",
								title: "提现成功",
								duration: 2000,
								position: 'bottom'
							})
							that.updateUserInfo(res.data)
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 1500)
						} else {
							uni.showToast({
								icon: "none",
								title: res.message,
								duration: 2000,
								position: 'bottom'
							})
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f5f5f5;
	}

	.tixian {
		/*  background-color: white; */
		display: flex;
		align-items: center;
		min-height: 100rpx;
		margin-top: 20rpx;
		padding-left: 20rpx;
		border-radius: 15rpx;
		background-color: #fff;
		justify-content: space-between;
	}

	.tixian input {
		flex: 1;
		font-size: 32rpx;
		color: #555;
		padding-right: 20rpx;
		/* background-color: #fff; */
	}

	/* .cu-form-group+.cu-form-group {
	 border-bottom: 1rpx solid #eee;
	 border-top: 1rpx solid #eee;
 } */
	.tixian .title {
		text-align: justify;
		padding-right: 30rpx;
		font-size: 30rpx;
		position: relative;
		height: 50rpx;
		color: #817d7d;
		line-height: 50rpx;
		/*  background-color: #ddc9c9; */
	}

	.shi {
		display: block;
		float: left;
		width: 10rpx;
		height: 30rpx;
		margin-top: 10rpx;
		margin-right: 10rpx;
		border-radius: 20rpx;
		background-color: #3ea2ff;
	}

	/* 提现按钮 */
	.text {
		width: 100%;
		height: 100rpx;
		margin-top: 150rpx;
	}

	.litext {
		color: rgb(184, 179, 179);
		font-size: 30rpx;
	}

	.chongtext {
		width: 260rpx;
		height: 80rpx;
		font-size: 32rpx;
		line-height: 80rpx;
		text-align: center;
		color: white;
		margin: 0 auto;
		border-radius: 2em;
		border: 1px solid #f7f5f5;
		background-color: #3ea2ff;
		box-shadow: inset 2px 2px 2px 0px rgba(255, 255, 255, 0.5),
			7px 7px 20px 0px rgba(0, 0, 0, 0.1), 4px 4px 5px 0px rgba(0, 0, 0, 0.1);
	}

	.chongtext2 {
		width: 260rpx;
		height: 80rpx;
		font-size: 34rpx;
		line-height: 80rpx;
		text-align: center;
		color: white;
		margin: 0 auto;
		border-radius: 2em;
		border: 1px solid #f7f5f5;
		background-color: #3ea2ff;
		box-shadow: inset 2px 2px 2px 0px rgba(255, 255, 255, 0.5),
			7px 7px 20px 0px rgba(0, 0, 0, 0.1), 4px 4px 5px 0px rgba(0, 0, 0, 0.1);
	}

	.tips {
		font-size: 25rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #808080;
	}
</style>
